package com.heer.gymnasium.module.reservation.views;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.ListView;

import com.heer.gymnasium.R;
import com.heer.gymnasium.api.ResponseData.ApiResponse;
import com.heer.gymnasium.api.callback.BaseCallback;
import com.heer.gymnasium.base.BaseRefreshActivity;
import com.heer.gymnasium.constant.Type;
import com.heer.gymnasium.module.main.views.MainActivity;
import com.heer.gymnasium.module.reservation.beans.ReservationItemBean;
import com.heer.gymnasium.module.reservation.beans.ReservationItemResponseBean;
import com.orangegangsters.github.swipyrefreshlayout.library.SwipyRefreshLayoutDirection;
import com.zhihuiyun.library.commonadapter.QuickTypeAdapter;
import com.zhihuiyun.library.commonadapter.ViewHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import retrofit2.Call;
import retrofit2.Response;

/**
 * Created by yan
 * on 2018/1/25
 * 订场列表
 */

public class ReservationListActivity extends BaseRefreshActivity {


    private static final int LAYOUT = R.layout.layout_reservation_list;
    private static final int LAYOUT_REFRESH = R.id.layout_reservation_list_srl;
    private static final int LAYOUT_LIST = R.id.layout_reservation_list_lv;

    private static final int ITEM_LAYOUT = R.layout.item_reservation_list;
    private static final int ITEM_ROOT_ID = R.id.item_reservation_list_rl;
    private static final int ITEM_TIME = R.id.item_reservation_list_time_tv;
    private static final int ITEM_NUM = R.id.item_reservation_list_num_tv;
    @BindView(LAYOUT_LIST)
    ListView listView;

    QuickTypeAdapter<ReservationItemBean> adapter;
    private List<ReservationItemBean> data;
    private boolean isFirst = true;

    private String cateId;//排期id
    private int reservationType;

    public static void startActivity(Context context, String cateId, int reservationType) {
        Intent intent = new Intent(context, ReservationListActivity.class);
        intent.putExtra(EXTRA_ID, cateId);
        intent.putExtra(EXTRA_TYPE, reservationType);
        context.startActivity(intent);
    }


    public static void startActivity(Activity activity, String cateId) {
        startActivity(activity, cateId, Type.RESERVATION_TYPE_NORMAL);
    }

    @Override
    protected void init() {
        super.init();
        setContentView(LAYOUT);
        setRefreshResId(LAYOUT_REFRESH);
        cateId = getIntent().getStringExtra(EXTRA_ID);
        reservationType = getIntent().getIntExtra(EXTRA_TYPE, Type.RESERVATION_TYPE_NORMAL);
    }

    @Override
    protected void initView() {
        super.initView();
        titleUtils.setTitleText("订场");
        titleUtils.startMainActivity(this);
        data = new ArrayList<>();
        adapter = new QuickTypeAdapter<ReservationItemBean>(this, data, ITEM_LAYOUT) {
            @Override
            public void bindData4View(ViewHolder holder, final ReservationItemBean data, int type, final int pos) {
                holder.setText(ITEM_TIME, data.getTime());
                holder.setText(ITEM_NUM, data.getStore_num() + "场可定");
                holder.setLis(ITEM_ROOT_ID, new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        ReservationActivity.startActivity((Activity) mContext, pos, cateId, reservationType);
                    }
                });
            }
        };
        listView.setAdapter(adapter);
    }

    @Override
    protected void loadData(boolean loadMore) {
        super.loadData(loadMore);
        isFirst = false;
        if (!loadMore) {
            data.clear();
        }

        layoutRefresh.setDirection(SwipyRefreshLayoutDirection.TOP);

        getBookintTimeList();
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (!isFirst) {
            loadData(false);
        }
    }

    private void getBookintTimeList() {
        mAppHelper.apiService().getBookintTimeList(cateId).enqueue(new BaseCallback<ApiResponse<List<ReservationItemResponseBean>>>() {

            @Override
            public void onSuccess(Call<ApiResponse<List<ReservationItemResponseBean>>> call, Response<ApiResponse<List<ReservationItemResponseBean>>> response) {
                data.addAll(ReservationItemBean.dataChange(response.body().getData()));
                adapter.notifyDataSetChanged();
                layoutRefresh.setRefreshing(false);
            }

            @Override
            public void onFailure(Call<ApiResponse<List<ReservationItemResponseBean>>> call, Throwable t) {
                super.onFailure(call, t);
                layoutRefresh.setRefreshing(false);
            }
        });
    }
}
